package com.rico.web.common;

import com.alibaba.fastjson.JSON;

import java.io.Serializable;

public class RespData<T> implements Serializable {
	 /**
     * description:序列化
     * remark:
     */
    private static final long serialVersionUID = -1;

    /**
     * 业务接口返回编码
     */
    private Integer code = 0;

    /**
     * 消息
     */
    private String message;

    /*
     * 模块名，如（SYS,PMS,MMS,OMS等）
     */
    private String module;
    /**
     * 返回对象
     */
    private T data;

    /**
     * 总条数
     */
    private Long totalCount;

    public RespData() {
        this.code = 0;
        this.message = "操作成功";
    }

    public Integer getCode() {
        return this.code;
    }

    public void setCode(final Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return this.message;
    }

    public void setMessage(final String message) {
        this.message = message;
    }

    public T getData() {
        return this.data;
    }

    public void setData(final T data) {
        this.data = data;
    }

    public Long getTotalCount() {
        return this.totalCount;
    }

    public void setTotalCount(final Long totalCount) {
        this.totalCount = totalCount;
    }

    public boolean isSuccess() {
        return 0 == this.code;
    }

    public RespData<T> setError(final Integer code, final String message) {
        this.code = code;
        this.message = message;
        return this;
    }

    public static <T> RespData<T> build() {
        return new RespData<T>();
    }

    public static <T> RespData<T> build(final T data, final Long totalCount) {
        final RespData<T> result = new RespData<T>();
        result.setData(data);
        result.setTotalCount(totalCount);
        return result;
    }

    public static <T> RespData<T> build(final T data) {
        final RespData<T> result = new RespData<T>();
        result.setData(data);
        return result;
    }

    public static <T> RespData<T> build(final Integer code, final String message) {
        final RespData<T> result = new RespData<T>();
        result.setError(code, message);
        return result;
    }

    public static <T> RespData<T> build(boolean flag) {
        final RespData<T> result = new RespData<T>();
        return flag ? RespData.build() : result.setError(-1, "操作失败");
    }

    public static <T> RespData<T> build(final Integer code, final String message, final T data) {
        final RespData<T> result = new RespData<T>();
        result.setError(code, message);
        result.setData(data);
        return result;
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }
}
